<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 跳格子1（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>小明和朋友玩跳格子游戏&#xff0c;有 n 个连续格子&#xff0c;每个格子有不同的分数&#xff0c;小朋友可以选择以任意格子起跳&#xff0c;但是不能跳连续的格子&#xff0c;也不能回头跳&#xff1b;</p> 
<p>给定一个代表每个格子得分的非负整数数组&#xff0c;计算能够得到的最高分数。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>给定一个数列&#xff0c;如&#xff1a;1 2 3 1</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出能够得到的最高分&#xff0c;如&#xff1a;4</p> 
<p></p> 
<h4>备注</h4> 
<p>1 ≤ nums.length ≤ 100<br /> 0 ≤ nums[i] ≤ 1000</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1 2 3 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">4</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">选择跳第一个格子和第三个格子</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2 7 9 3 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">12</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">2&#43;9&#43;1&#61;12</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题是<a href="https://blog.csdn.net/qfc_128220/article/details/129471616?spm&#61;1001.2014.3001.5501" title="LeetCode - 198 打家劫舍_伏城之外的博客-CSDN博客">LeetCode - 198 打家劫舍_伏城之外的博客-CSDN博客</a>换皮题</p> 
<p>解析可以看链接博客。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const nums &#61; line.split(&#34; &#34;).map(Number);
  console.log(getResult(nums));
});

function getResult(nums) {
  const n &#61; nums.length;

  const dp &#61; new Array(n).fill(0);

  if (n &gt;&#61; 1) dp[0] &#61; nums[0];
  if (n &gt;&#61; 2) dp[1] &#61; Math.max(nums[0], nums[1]);

  for (let i &#61; 2; i &lt; n; i&#43;&#43;) {
    dp[i] &#61; Math.max(dp[i - 1], dp[i - 2] &#43; nums[i]);
  }

  return dp[n - 1];
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int[] nums &#61; Arrays.stream(sc.nextLine().split(&#34; &#34;)).mapToInt(Integer::parseInt).toArray();

    System.out.println(getResult(nums));
  }

  public static int getResult(int[] nums) {
    int n &#61; nums.length;

    int[] dp &#61; new int[n];

    if (n &gt;&#61; 1) dp[0] &#61; nums[0];
    if (n &gt;&#61; 2) dp[1] &#61; Math.max(nums[0], nums[1]);

    for (int i &#61; 2; i &lt; n; i&#43;&#43;) {
      dp[i] &#61; Math.max(dp[i - 1], dp[i - 2] &#43; nums[i]);
    }

    return dp[n - 1];
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
nums &#61; list(map(int, input().split()))


# 算法入口
def getResult():
    n &#61; len(nums)

    dp &#61; [0] * n

    if n &gt;&#61; 1:
        dp[0] &#61; nums[0]

    if n &gt;&#61; 2:
        dp[1] &#61; max(nums[0], nums[1])

    for i in range(2, n):
        dp[i] &#61; max(dp[i - 1], dp[i - 2] &#43; nums[i])

    return dp[n - 1]


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 100

#define MAX(a,b) a &gt; b ? a : b

int getResult(int* nums, int nums_size);

int main()
{
	int nums[MAX_SIZE];
	int nums_size &#61; 0;
	while(scanf(&#34;%d&#34;, &amp;nums[nums_size&#43;&#43;])) {
		if(getchar() !&#61; &#39; &#39;) break;
	}
	
	printf(&#34;%d\n&#34;, getResult(nums, nums_size));
	
	return 0;
}

int getResult(int* nums, int nums_size)
{
	int dp[MAX_SIZE] &#61; {0};
	
	if(nums_size &gt;&#61; 1) {
		dp[0] &#61; nums[0];
	}
	
	if(nums_size &gt;&#61; 2) {
		dp[1] &#61; MAX(nums[0], nums[1]);
	}
	
	for(int i&#61;2; i&lt;nums_size; i&#43;&#43;) {
		dp[i] &#61; MAX(dp[i-1], dp[i-2] &#43; nums[i]);
	}
	
	return dp[nums_size-1];
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>